package net.yla.smartbus.module.controller.web.ba.duty;

import com.alibaba.druid.sql.visitor.functions.If;
import net.yla.smartbus.core.api.ApiResult;
import net.yla.smartbus.core.base.BaseController;
import net.yla.smartbus.core.constants.UserRoleEnum;
import net.yla.smartbus.module.ba.dept.query.DeptEmpQuery;
import net.yla.smartbus.module.ba.duty.form.DutyForm;
import net.yla.smartbus.module.ba.duty.query.DutyQuery;
import net.yla.smartbus.module.ba.duty.service.DutyService;
import net.yla.smartbus.module.ba.right.roles.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;

/**
 * @description:
 * @author: Yee
 * @date: 2021/6/17 14:59
 */
@RestController
@RequestMapping("/api/web/duty")
public class DutyController extends BaseController {
    @Autowired
    private DutyService dutyService;
    @Autowired
    private RolesService rolesService;

    /**
     * 分页查询职务
     * @param query
     * @return
     */
    @PostMapping("/page/query")
    public ApiResult pageDuties(@RequestBody DutyQuery query){
        rolesService.userRoleEvaluation(UserRoleEnum.WORK_SELECT,getToken(),getIpAddress());
        return success(dutyService.listDuties(query));
    }

    /**
     * 逻辑删除表单数据
     * @param query
     * @return
     */
    @PostMapping("/delete")
    public ApiResult delete(@RequestBody DutyQuery query){
        rolesService.userRoleEvaluation(UserRoleEnum.WORK_DELETE,getToken(),getIpAddress());
        return success(dutyService.delete(query.getDutyId()));
    }

    /**
     * 新增和修改职务表单
     * @param form
     * @return
     */
    @PostMapping("/update")
    public ApiResult updateDuty(@RequestBody DutyForm form){
        if(form.getDutieId() != null){
            rolesService.userRoleEvaluation(UserRoleEnum.WORK_UPDATE,getToken(),getIpAddress());
        }else {
            rolesService.userRoleEvaluation(UserRoleEnum.WORK_INSERT,getToken(),getIpAddress());
        }
        return success(dutyService.updateDuty(form));
    }

    /**
     * 查询所有部门
     * @param query
     * @return
     */
    @PostMapping("/allDept")
    public ApiResult allDept(@RequestBody DeptEmpQuery query){
        return success(dutyService.allDept(query));
    }


}
